package cn.hxzy.assignperson.controller;

import cn.hxzy.assignperson.service.TeacherMajorService;
import cn.hxzy.common.domain.*;
import cn.hxzy.common.domain.ClassTable;
import cn.hxzy.common.domain.vo.*;
import cn.hxzy.common.response.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/teachermajor")
public class TeacherMajorController {

    @Autowired
    private TeacherMajorService teacherMajorService;

    //    查询所有老师的所属专业
    @RequestMapping("/selectAllGl")
    public R selectAllGl() {

        List<FuzeVo> teacherMajors = teacherMajorService.selectAllGl();
        return R.success(teacherMajors);
    }

    //    查询所有学校信息
    @RequestMapping("/selectAllSchool")
    public R selectAllSchool() {
        List<School> schools = teacherMajorService.selectAllSchool();
        return R.success(schools);
    }

    //    新增老师以及sys_teacher_major
    @RequestMapping("/insertUser")
    @Transactional
    public R insertUser(@RequestBody InsertVo insertVo) {
        int i = teacherMajorService.insertUser(insertVo);
        if (i > 0) {
            return R.success("新增成功");
        }

        return R.error(400, "新增失败");
    }

    //查询单个老师信息包括专业
    @RequestMapping("/selectByIdall/{id}")
    public R selectByIdall(@PathVariable Integer id) {
        InsertVo insertVos = teacherMajorService.selectByIdall(id);
        return R.success(insertVos);
    }

    //    修改老师信息
    @Transactional
    @RequestMapping("/updateUser")
    public R updateUser(@RequestBody InsertVo insertVo) {
        int i = teacherMajorService.updateUser(insertVo);
        if (i > 0){
            return R.success("修改成功");
        }else if (i == -1) {
            return R.error(400, "该账号已存在");
        }else {
            return R.error(400, "修改失败");
        }
    }

    //    删除
    @RequestMapping("/deleteTeacherMajor/{id}")
    public R deleteTeacherMajor(@PathVariable Integer id) {
        int i = teacherMajorService.deleteTeacherMajord(id);
        if (i > 0) {
            return R.success("删除成功");
        } else if (i==-1) {
            return R.error(400,"该专业已有班级，暂时无法删除");

        }else if (i==-2){
            return R.error(400, "该专业已有课程，暂时无法删除");
        }else if (i==-3){
            return R.error(400, "此专业正在被试卷使用，暂时无法删除");
        }else if (i==-4){
            return R.error(400, "此专业正在被试题使用，暂时无法删除");
        }else if (i==-5){
            return R.error(400, "此专业正在被标签使用，暂时无法删除");

        }else if (i==-6){
            return R.error(400, "此专业有相关的老师信息，暂时无法删除");
        } else if (i==-7) {
            return R.error(400, "此专业下还存在学生，转班后重试");

        } else {
            return R.error(400, "删除失败");
        }
    }

    //    查询专业
//    @RequestMapping("/selectAllDirection")
//    public R selectAllDirection() {
//        List<Direction> directions = teacherMajorService.selectAllDirection();
//        return R.success(directions);
//    }

    //    查询所有老师名称
    @RequestMapping("/selectAllTeacher/{mid}")
    public R selectAllTeacher(@PathVariable Integer mid) {
        List<User> users = teacherMajorService.selectAllTeacher(mid);
        return R.success(users);
    }

    //    专业组组长
    @RequestMapping("/selectteacherGl")
    public R selectteacherGl() {
        List<TeacherVo01> teacherVo01s = teacherMajorService.selectteacherGl();
        return R.success(teacherVo01s);
    }

    //    添加专业
    @RequestMapping("/addDirection/{directionName}")

    public R addDirection(@PathVariable String directionName) {
        int i = teacherMajorService.addDirection(directionName);
        if (i > 0) {
            return R.success("添加成功");
        }
        return R.error(400, "添加失败");
    }

    //    修改专业
    @RequestMapping("/updateDirection")
    public R updateDirection(@RequestBody Direction direction) {
        int i = teacherMajorService.updateDirection(direction);
        if (i > 0) {
            return R.success("修改成功");
        }
        return R.error(400, "修改失败");
    }

    //修改教研组
    @RequestMapping("/updateTeacheringResearchId/{userId}/{mid}")
    public R updateTeacheringResearchId(@PathVariable Integer userId
            , @PathVariable Integer mid) {
        teacherMajorService.updateTeacheringResearchId(userId, mid);

        return R.success("修改成功");

    }


//管理员向上
//===============================================================================================================
//教研组组长向下

    //教研组班级管理页面查询班级信息
    @RequestMapping("/selectallbymajorid/{id}")
    public R selectAllByMajorId(@PathVariable Integer id) {
        List<ClassTableVo> classTableVos = teacherMajorService.selectAllClassByMarjorId(id);
        return R.success(classTableVos);
    }


    //根据专业id查询老师相关信息
    @RequestMapping("/selectTeacherMessage/{tid}")
    public R selectTeacherNames(@PathVariable Integer tid) {
        List<TeacherVo> selectteachers = teacherMajorService.selectteachers(tid);
        return R.success(selectteachers);
    }

    //修改班级的老师
    @RequestMapping("/fenpeiTeacher")
    public R updateTeacher(@RequestBody Map<String, Object> map) {
        List<Integer> teacherlist = (List<Integer>) map.get("teachersId");

        if (teacherlist == null || teacherlist.isEmpty()) {
            // 为空说明没进行修改
            return R.error(500, "暂无任何修改");
        } else {
            try {
                Integer i = teacherMajorService.addOrUpdate(map);
                if (i == null || i <= 0) {
                    return R.error(500, "修改失败");
                }
            } catch (Exception e) {
                e.printStackTrace();
                return R.error(500, "系统错误: " + e.getMessage());
            }
        }
        return R.success("分配成功");
    }

    //创建班级
    @RequestMapping("/addClass")
    public R addClass(@RequestBody ClassTable classTable) {
        Integer i = teacherMajorService.addOneClass(classTable);
        if (i > 0) {
            return R.success("创建班级成功");
        } else if (i == -1) {
            return R.error(250, "班级名已存在");
        } else {
            return R.error(500, "创建班级失败");
        }

    }

    //修改班级信息
    @RequestMapping("/changeOneClass")
    public R changeOneClass(@RequestBody ClassTable classTable) {
        Integer i = teacherMajorService.updateOneClass(classTable);

        if (i > 0) {

            return R.success("修改成功");

        } else if (i == -1) {
            return R.error(250, "修改失败 已存在同名班级");
        } else {
            return R.error(500, "修改班级失败");
        }


    }

    //    查询专业名称
    @RequestMapping("/selectDirectionById/{id}")
    public R selectDirectionById(@PathVariable Integer id) {
        Direction directionById = teacherMajorService.selectDirectionById(id);
        return R.success(directionById);
    }

    //根据专业id查询老师信息
    @RequestMapping("/selectTeacherByMarjorId/{id}")
    public R selectTeacherByMarjorId(@PathVariable Integer id) {
        List<TeacherVo> Teachers = teacherMajorService.selectTeacherByMarjorId(id);
        return R.success(Teachers);
    }


}
